package com.hdax.dm.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 从rabbitmq获取消息后进行短信发送,将此类设置为监听类
 */
//必须是组件类
@Component
//监听 Queue 里面的消息,需配合 @RabbitHandler 注解一起使用
@RabbitListener(queues = {"dmsms"})
public class VcodeService {

    @RabbitHandler
    public void sendSms(Map<String,String> messages){
        String code = messages.get("vcode");
        String phone = messages.get("phoneNumber");
        String accessKeyId = "LTAI4Fi6Dyp85rtARLMG1WoB";
        String accessKeySecret = "3qu7gkF1ZQqJ8bh4lWMgLewUkq48ks";
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "邯郸翱翔");
        request.putQueryParameter("TemplateCode","SMS_180346654");
        request.putQueryParameter("TemplateParam", " {\"code\":\""+code+"\"}");
            try {
                CommonResponse response = client.getCommonResponse(request);
                System.out.println(response.getData());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
    }
}
